package com.flyu.myThread.Syn;

public class SynBank {
    public static void main(String[] args) {
        Account2 account2 = new Account2("结婚基金", 1000);
        Drawing2 you = new Drawing2(account2, 50, "你");
        Drawing2 girl = new Drawing2(account2, 100, "girl");

        you.start();
        girl.start();
    }
}


class Account2{
    String name;
    int money;

    public Account2(String name, int money){
        this.name = name;
        this.money = money;
    }
}

class Drawing2 extends Thread{

    Account2 account2;
    int drawingMoney2;
    int nowMoney2;

    public Drawing2(Account2 account2, int drawingMoney2, String name){
        super(name);
        this.account2 = account2;
        this.drawingMoney2 = drawingMoney2;
    }

    @Override
    public void run() {
        synchronized (account2){
            if (account2.money < drawingMoney2){
                System.out.println("钱不够，取不了");
                return;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 卡内余额
            account2.money = account2.money-drawingMoney2;
            // 手里的钱
            nowMoney2 = nowMoney2 + drawingMoney2;
            System.out.println(this.getName()+"手里的钱为"+nowMoney2);
            System.out.println(account2.name+"余额为"+account2.money);
        }

    }
}